package cn.com.blueInfo.core.baseService.service;

import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.dnspod.v20210323.DnspodClient;
import com.tencentcloudapi.dnspod.v20210323.models.DescribeRecordListRequest;
import com.tencentcloudapi.dnspod.v20210323.models.DescribeRecordListResponse;
import com.tencentcloudapi.dnspod.v20210323.models.RecordListItem;

import javax.servlet.ServletContext;
import java.util.Timer;

public class DDNSTimerService {
    /** 每天执行一次 */
    private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;
    /** 每小时执行一次 */
    private static final long PERIOD_HOUR = 60 * 60 * 1000;
    /** 每分钟执行一次 */
    private static final long PERIOD_MINUTE = 60 * 1000;
    /** 每五分钟执行一次 */
    private static final long PERIOD_FIVE_MINUTE = 5 * 60 * 1000;
    /** 每十分钟执行一次 */
    private static final long PERIOD_TEN_MINUTE = 10 * 60 * 1000;
    /** 每十五分钟执行一次 */
    private static final long PERIOD_FIFTEEN_MINUTE = 15 * 60 * 1000;

    public DDNSTimerService(ServletContext servletContext) {
        this.updateDNS(servletContext);
    }

    private void updateDNS(ServletContext servletContext) {
        Timer timer = new Timer();

        //UpdateDNS updateDNS = new UpdateDNS(servletContext);
        //timer.schedule(updateDNS, 0, PREIOD_TEN_MINUTE);
    }

    /**
     * 创建客户端
     * @return
     */
    private static DnspodClient createAliyunClient() {
        String secretId = System.getenv("SECRET_ID");
        String secretKey = System.getenv("SECRET_KEY");
        Credential cred = new Credential(secretId, secretKey);
        DnspodClient client = new DnspodClient(cred, "");
        return client;
    }

    /**
     * 查询域名列表
     * @param client
     * @param domain
     * @return
     */
    private static RecordListItem queryDomainList(DnspodClient client, String domain) {
        RecordListItem modifyItem = null;
        try {
            // 查询域名的解析记录列表
            DescribeRecordListRequest queryRequest = new DescribeRecordListRequest();
            queryRequest.setDomain(domain); // 域名

            DescribeRecordListResponse queryResponse = client.DescribeRecordList(queryRequest);
            RecordListItem[] listItems =  queryResponse.getRecordList();
            modifyItem = listItems[2];
            System.out.println(AbstractModel.toJsonString(queryResponse));
        } catch (TencentCloudSDKException e) {
            System.err.println(e.toString());
            domain = domain.substring(domain.indexOf(".") + 1);
            queryDomainList(client, domain);
        }
        return modifyItem;
    }

    /**
     * 注释掉的部分是修改记录的代码，TODO 直接抽出方法即可使用
     * @param args
     */
    public static void main(String[] args) {
        DnspodClient client = createAliyunClient();

        RecordListItem modifyItem = queryDomainList(client, "ipv6.test.suxch.online");
//        try{
//
//            // 修改解析记录
//            ModifyRecordRequest modifyRequest = new ModifyRecordRequest();
//            modifyRequest.setDomain("suxch.online"); // 域名
//            modifyRequest.setRecordType(modifyItem.getType()); // 记录类型，通过 API 记录类型获得，大写英文，比如：A 。
//            modifyRequest.setRecordLine(modifyItem.getLine()); // 记录线路，通过 API 记录线路获得，中文，比如：默认。
//            modifyRequest.setValue("192.168.0.0"); // 记录值，如 IP : 200.200.200.200， CNAME : cname.dnspod.com.， MX : mail.dnspod.com.。
//            modifyRequest.setRecordId(modifyItem.getRecordId()); // 记录 ID 。可以通过接口DescribeRecordList查到所有的解析记录列表以及对应的RecordId
//            ModifyRecordResponse modifyResponse = client.ModifyRecord(modifyRequest);
//
//        } catch (TencentCloudSDKException e) {
//            System.err.println(e.toString());
//        }
    }

}
